package com.bnlr.lrparking.appui.mine.personal;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.constant.Urls;
import com.bnlr.lrparking.util.CommonUtil;
import com.bnlr.lrparking.view.ClearEditText;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.yolanda.nohttp.NoHttp;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 修改昵称
 *
 * @author tian
 */
public class AlterNickNameActivity extends BaseActivity {
    @BindView(R.id.tv_nav_title)
    TextView tvNavTitle;
    @BindView(R.id.tv_nav_right)
    TextView tvNavRight;
    @BindView(R.id.et_alter_name)
    ClearEditText personNickname;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alter_nick_name);
        ButterKnife.bind(this);
        tvNavTitle.setText("修改昵称");
        tvNavRight.setText("保存");
        if (TextUtils.isEmpty(AppConfig.getInstance().getString("nickName", ""))) {
            personNickname.setText(AppConfig.getInstance().getString("userName", ""));
        } else {
            personNickname.setText(AppConfig.getInstance().getString("nickName", ""));
        }
        personNickname.setSelection(personNickname.length());
    }

    @Override
    public void doClick(View v) {
        super.doClick(v);
        switch (v.getId()) {
            case R.id.tv_nav_right:
                if (TextUtils.isEmpty(personNickname.getText().toString())) {
                    ToastUtils.showShort("昵称不能为空");
                    return;
                }
                sendName();
                break;
            default:
                break;
        }
    }
    private void sendName() {
        try {
            if (TextUtils.isEmpty(personNickname.getText().toString())) {
                CommonUtil.showToask(baseContext, "用户昵称不能为空");
                return;
            }
            mRequest = NoHttp.createStringRequest(Urls.IP, LrConstant.POST);
            mRequest.add("action", "UpdateNickNameByUserId");
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            mRequest.add("name", personNickname.getText().toString());
            // 添加到请求队列+
            CallServer.getRequestInstance().add(baseContext, mRequest,
                    new CustomHttpListener<JSONObject>(baseContext, true, null) {
                        @Override
                        public void doWork(JSONObject data, String code) {

                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                super.onFinally(obj, code, isSucceed);
                                if (TextUtils.equals("1", code)) {
                                    CommonUtil.showToask(baseContext, "修改成功");
                                    AppConfig.getInstance().putString("nickName", obj.getJSONObject("data").getString("nickname"));
                                    finish();
                                } else {
                                    CommonUtil.showToask(baseContext, obj.getString("msg"));
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }, true);
        } catch (Exception e) {

        }
    }
}
